<%-- 
    Document   : test
    Created on : 07.01.2013, 22:54:10
    Author     : Администратор
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&region=UA"></script>
    <script type="text/javascript" src="<c:url value="/resources/js/jquery.min.js"/>"></script>
    <link id="switch_style" href="<c:url value="/resources/css/real_estate.css"/>" rel="stylesheet">
    <!--<script type="text/javascript" src="<c:url value="/resources/js/jquery.geocomplete.js"/>"></script>-->
    <script type="text/javascript" src="<c:url value="/resources/js/belkodevelop/geoinput.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/resources/js/belkodevelop/geomap.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/resources/bootstrap/js/bootstrap.js"/>"></script>
    <script type="text/javascript">
        $(function() {
            $.geoinput({
                serviceUrl: '<c:url value="/api/geocoding/json"/>'
            });
            
            $("#map_canvas").geomap();
        });
    </script>
    </head>
    <body>

    <div id="map_canvas" style="width:350px; height:250px;"></div>

    <div id="header_wrap" class="outer">
        <header class="inner">
          <h1 id="project_title">Geocomplete</h1>
          <h2 id="project_tagline">Example: Simple (No Map)</h2>
        </header>
    </div>

    <div id="main_content_wrap" class="outer">
      <section id="main_content" class="inner">

        

          <input id="geocomplete" type="text" placeholder="Type in an address" size="250"  />
          <input id="find" type="button" value="find" />



        
      </section>
    </div>

    

    
    <script>
      $(function(){

        /*var input = $('#geocomplete')[0];
        var options = {
            types: ['geocode'],
            componentRestrictions: {country: 'ua'}
        };

        autocomplete = new google.maps.places.Autocomplete(input, options);
        geocoder = new google.maps.Geocoder();
        
        $(input).keypress(function(event){
            if (event.keyCode === 13){ return false; }
        });
        
        function update(place) {
            setTimeout(function(){$(input).val("ffff");}, 50);
        }
        
        function find(adress) {
            var request = {adress: adress};
            geocoder.geocode(request, function(result, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    var result = results[0];
                    alert(result.formatted_address);
                    update(result);
                    if (results.length > 1){
                        this.trigger("geocode:multiple", results);
                    }
                } else {
                    this.trigger("geocode:error", status);
                }
            });
        }
        
        google.maps.event.addListener(autocomplete, 'place_changed', function(el) {
            var place = autocomplete.getPlace();
            console.log(place);
            console.log(autocomplete);
            // update the textboxes
            
            if (!place.geometry){
                find(place.name);
            } else {
                update(place);
            }

        });
        
        */
        
        
        
        
        /*
        
        
geocoder = new google.maps.Geocoder();

function getSimpleAdressFormat(place) {
      var data ={};
      $.each(place.address_components, function(index, object){
        var name = object.types[0];
        data[name] = object.long_name;
        data[name + "_short"] = object.short_name;
      });
      return data;
}

function addressFormat(place) {
    var priority = ['street_number', 'route', 'sublocality', 'locality', 
                    'administrative_area_level_3', 'administrative_area_level_1',
                'country', 'postal_code'];
    priority.reverse();
    var undesirableElements = ['administrative_area_level_3', 'country', 'postal_code', 'sublocality'];
    
    var simplePlace = getSimpleAdressFormat(place);
    var result = "";
    for (var i in priority) {
        if ($.inArray(priority[i], undesirableElements) < 0) {
            if (simplePlace[priority[i]]) {
                if (result) {
                    if (priority[i] == 'street_number') {
                        result += " дом ";
                    } else {
                        result += ", ";
                    }
                }
                result += simplePlace[priority[i]];
            }
        }
    }
    return result;
}

function getCountry(place) {
    var simplePlace = getSimpleAdressFormat(place);
    return simplePlace.country_short;
}
        
  $.fn.geoinput = function (option) {
    return this.each(function () {
      var $this = $(this);
        $this.typeahead({
                //источник данных
                minLength: 0,
                items: 10,
                insertValue: false,
                source: function (query, process) {
                    var request = {'address': query, 'region': 'ua'};
                    console.log(request);
                    geocoder.geocode(request, function(results, status) {
                        if (status === google.maps.GeocoderStatus.OK) {
                            var data = new Array();
                            $.each(results, function(i, place) {
                                if (getCountry(place) == "UA") {
                                    place.string = addressFormat(place);
                                    data.push(place);
                                }
                            });
                            console.log(data);
                            process(data);
                        } else {
                            throw "Error geocode status " + status;
                        }
                    });


                },
                matcher: function() {
                    return true;
                },
                updater: function(item) {
                    console.log();
                    return item;
                },
                highlighter: function(item) {
                    return item;
                },
                sorter: function (items) {
                    var beginswith = []
                        , caseSensitive = []
                        , caseInsensitive = []
                        , item

                    while (item = items.shift()) {
                        var strItem = item.string;
                        if (!strItem.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(strItem)
                        else if (~strItem.indexOf(this.query)) caseSensitive.push(strItem)
                        else caseInsensitive.push(strItem)
                    }

                    return beginswith.concat(caseSensitive, caseInsensitive)
                    }
        });
      if (typeof option == 'string') $this.typeahead(option);
    })
  }
  */
$('#geocomplete').geoinput();
        
/*$('#geocomplete').typeahead({
        //источник данных
        minLength: 0,
        items: 10,
        insertValue: false,
        test: function() {
            alert("test f");
        },
        source: function (query, process) {
            var request = {'address': query, 'region': 'ua'};
            console.log(request);
            geocoder.geocode(request, function(results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    var data = new Array();
                    $.each(results, function(i, place) {
                        if (getCountry(place) == "UA") {
                            place.string = addressFormat(place);
                            data.push(place);
                        }
                    });
                    console.log(data);
                    process(data);
                } else {
                    throw "Error geocode status " + status;
                }
            });
           

          },
          matcher: function() {
              return true;
          },
          updater: function(item) {
              return item;
          },
          highlighter: function(item) {
              return item;
          },
        sorter: function (items) {
            var beginswith = []
                , caseSensitive = []
                , caseInsensitive = []
                , item

            while (item = items.shift()) {
                var strItem = item.string;
                if (!strItem.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(strItem)
                else if (~strItem.indexOf(this.query)) caseSensitive.push(strItem)
                else caseInsensitive.push(strItem)
            }

            return beginswith.concat(caseSensitive, caseInsensitive)
            }
});*/
      });
    </script>
    </body>

